﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.BusinessLogic;
using DiscoveryLogic.Analytics.BusinessLogic.Networks;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Analytics.Modules.Explorers;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.DataNavigation
{
    /// <summary>
    /// Interaction logic for NetworkTreeView.xaml
    /// </summary>
    public partial class NetworkTreeView : UserControl
    {
        public delegate void OnNetworkSelected(Network network);
        public delegate void OnViewNetworkGraph(Network network);
        public delegate void OnViewRadialGraph(Network network);
        public delegate void OnViewNetworkData(Network network);
        public delegate void OnViewNetworkStats(Network network);
        public event OnNetworkSelected NetworkSelected;
        public event OnViewNetworkGraph ViewNetworkGraph;
        public event OnViewRadialGraph ViewRadialGraph;
        public event OnViewNetworkData ViewNetworkData;
        public event OnViewNetworkStats ViewNetworkStats;

        private NetworkTreePresenter _Presenter;

        public NetworkTreeView()
        {
            InitializeComponent();
        }

        public NetworkTreeView(NetworkTreePresenter presenter):this()
        {
            this._Presenter = presenter;
            this._Presenter.View = this;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        public void PopulateTreeView(ObservableCollection<NetworksBySource> allNetworks)
        {
            this.treeNetworks.ItemsSource = allNetworks;
        }

        private void treeNetworks_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if(e.NewValue is Network)
            {
                Network network = (Network) e.NewValue;
                if (this.NetworkSelected != null)
                    this.NetworkSelected(network);
            }
        }

        #region context menu-view network
        private void cmuNetworkGraph_Click(object sender, RoutedEventArgs e)
        {
            if(this.treeNetworks.SelectedValue !=null && 
                this.treeNetworks.SelectedValue is Network)
            {
                Network network = this.treeNetworks.SelectedValue as Network;
                if (this.ViewNetworkGraph != null)
                    this.ViewNetworkGraph(network);
            }
        }

        private void cmuRadialGraph_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeNetworks.SelectedValue != null &&
                            this.treeNetworks.SelectedValue is Network)
            {
                Network network = this.treeNetworks.SelectedValue as Network;
                if (this.ViewRadialGraph != null)
                    this.ViewRadialGraph(network);
            }
        }

        private void cmuNetworkData_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeNetworks.SelectedValue != null &&
                this.treeNetworks.SelectedValue is Network)
            {
                Network network = this.treeNetworks.SelectedValue as Network;
                if (this.ViewNetworkData != null)
                    this.ViewNetworkData(network);
            }
        }

        private void cmuNetworkStats_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeNetworks.SelectedValue != null &&
                            this.treeNetworks.SelectedValue is Network)
            {
                Network network = this.treeNetworks.SelectedValue as Network;
                if (this.ViewNetworkStats != null)
                    this.ViewNetworkStats(network);
            }
        }
        #endregion

        #region context menu-explore network
        private void cmuGrantToPubs_Click(object sender, RoutedEventArgs e)
        {
            if(this.treeNetworks.SelectedValue !=null && 
                this.treeNetworks.SelectedValue is Network)
            {
                Network network = (Network) this.treeNetworks.SelectedValue;
                RuleType ruleType = RuleType.Link;
                EntityType tgtEntityType = EntityType.Publications;
                ExploreNetworkAssociation explorer=
                    new ExploreNetworkAssociation(network, ruleType, tgtEntityType);
                PopupWindow dialog=new PopupWindow();
                dialog.Content = explorer;
                dialog.Show();
            }
        }

        private void cmuGrantToPersons_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeNetworks.SelectedValue != null &&
                this.treeNetworks.SelectedValue is Network)
            {
                Network network = (Network)this.treeNetworks.SelectedValue;
                RuleType ruleType = RuleType.Link;
                EntityType tgtEntityType = EntityType.Persons;
                ExploreNetworkAssociation explorer = 
                    new ExploreNetworkAssociation(network, ruleType, tgtEntityType);
                PopupWindow dialog = new PopupWindow();
                dialog.Content = explorer;
                dialog.Show();
            }
        }

        private void cmuGrantToOrgs_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeNetworks.SelectedValue != null &&
                this.treeNetworks.SelectedValue is Network)
            {
                Network network = (Network)this.treeNetworks.SelectedValue;
                RuleType ruleType = RuleType.Link;
                EntityType tgtEntityType = EntityType.Organizations;
                ExploreNetworkAssociation explorer = 
                    new ExploreNetworkAssociation(network, ruleType, tgtEntityType);
                PopupWindow dialog = new PopupWindow();
                dialog.Content = explorer;
                dialog.Show();
            }
        }

        private void cmuPubToOrgs_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeNetworks.SelectedValue != null &&
                this.treeNetworks.SelectedValue is Network)
            {
                Network network = (Network)this.treeNetworks.SelectedValue;
                RuleType ruleType = RuleType.Link;
                EntityType tgtEntityType = EntityType.Organizations;
                ExploreNetworkAssociation explorer = 
                    new ExploreNetworkAssociation(network, ruleType, tgtEntityType);
                PopupWindow dialog = new PopupWindow();
                dialog.Content = explorer;
                dialog.Show();
            }
        }
        #endregion

        private void cmuDeleteNetwork_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeNetworks.SelectedValue != null &&
                this.treeNetworks.SelectedValue is Network)
            {
                Network network = (Network)this.treeNetworks.SelectedValue;
                NetworkManager.Delete(network);
                MyData.ReadAllNetworks();
            }
        }
    }
}
